package com.owen.cyclic.barrier.simple5;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class MyRunnable5 implements Runnable {

    private CyclicBarrier cyclicBarrier;
    private String name;
    private int sleep;

    public MyRunnable5(CyclicBarrier cyclicBarrier, String name, int sleep){
        this.cyclicBarrier=cyclicBarrier;
        this.name=name;
        this.sleep=sleep;
    }

    @Override
    public void run() {
        long startTime=0,endTime=0;
        try {
            TimeUnit.SECONDS.sleep(sleep);
            startTime=System.currentTimeMillis();
            if(name.equals("员工0")){
                cyclicBarrier.await(5,TimeUnit.SECONDS);
            }else{
                cyclicBarrier.await();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        endTime=System.currentTimeMillis();
        System.out.println(name+", sleep: "+sleep+" 等待了"+(endTime-startTime)+"(ms)，开始吃饭了！");
    }
}
